package com.ymatou.iapi.sellerproduct.testcase;

import java.util.HashMap;
import java.util.Map;

import org.json.JSONObject;
import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
import org.junit.experimental.categories.Category;

import com.ymatou.iapi.productmanager.parameter.product.AddSellerProductRequest;
import com.ymatou.iapi.productmanager.service.AddSellerProductCall;
import com.ymatou.iapi.sellerproduct.service.AddNewSellerProductCall;
import com.ymatou.iapi.sellerproduct.service.UpdateProductCatalogsCall;
import com.ymt.core.tool.Logger;
import com.ymt.utils.annotation.TestCase;
import com.ymt.utils.annotation.TestSuite;
import com.ymt.utils.tag.P1;
import com.ymttest.business.service.NewJavaMongoCheckService;
import com.ymttest.business.service.ProductManagerCallServiceV3;
import com.ymttest.business.service.SellerProductCallService;
import com.ymttest.database.mongo.YmtMongoDriverFactroy;
import com.ymttest.database.sqlwapper.YmtCatalogsIWapper;
import com.ymttest.utils.EnvSetup;
import com.ymttest.utils.JsonBeanHelper;
import com.ymttest.utils.MapUtil;
import com.ymttest.utils.constant.JsonBeanFile;
/**
*更新规格库存接口
*FreeMaker Template自动生成代码
*/
@TestSuite
public class Ts_UpdateProductCatalogs{
	private static UpdateProductCatalogsCall updateProductCatalogsCall = new UpdateProductCatalogsCall();

	private static String sellerName = EnvSetup.getData("productsellername");
	private static int sellerId = Integer.parseInt(EnvSetup.getData("productsellerid"));
	private static String activityStartId = EnvSetup.getData("productActivityStartId");
	private static String activityUser = EnvSetup.getData("admin");
	private static String activityPswd = EnvSetup.getData("password");
	
	@BeforeClass
	public static void setUp() {
		Logger.createResultFile("更新规格库存接口");
	}

	@Before
	public void caseUp() {
		
	}

	@AfterClass
	public static void tearDown() {
		Logger.generateResult(updateProductCatalogsCall.getOpurl());
	}

	@After
	public void caseDown() {
		Logger.end();
	}
	
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_UpdateProductCatalogs_001() {
		Logger.start(true,"修改单规格商品规格价格、库存");
		try {
			//定义期望值
			int Num=16;
			double QuotePrice=100.00;
			double VipPrice =20.00;
			double NewCustomerPrice=30.00; 
			
			//添加商品 -定时上架
			JSONObject productReqObject = SellerProductCallService.addNewProductOnTime(sellerId, sellerName, null);
			Logger.verifyEquals(productReqObject.getInt("Code"), 200, "响应报文-返回码");
			
			//获取返回报文的ProductID值
			String productID =  productReqObject.getJSONObject("Data").getString("ProductID");
			Logger.verifyNotNull(productID, "响应报文-商品ID");
			
			//获取CatalogsID 
			String catalogsID = new YmtCatalogsIWapper().selectCatalogsByProductId(productID).get(0).getsCatalogId();
			Logger.verifyNotNull(catalogsID, "响应报文-CatalogID");			
							
			//修改库存
			//参数赋值
			Map<String, Object> updateMap = new HashMap<String, Object>();
			updateMap.put("Catalogs[0]->UserId", sellerId);			
			updateMap.put("UserID", sellerId);
			updateMap.put("ProductID", productID);
			updateMap.put("Catalogs[0]->CatalogId", catalogsID);	
			updateMap.put("Catalogs[0]->Num", Num);
			updateMap.put("Catalogs[0]->Price", QuotePrice);
			updateMap.put("Catalogs[0]->VipPrice", VipPrice);
			updateMap.put("Catalogs[0]->NewCustomerPrice", NewCustomerPrice);
						
			JSONObject productCatalogsBean = JsonBeanHelper.getJsonBean(JsonBeanFile.updateProductCatalogsTpl);
			JsonBeanHelper.updateJsonBean(productCatalogsBean, updateMap);
								
			//call			
			JSONObject result = updateProductCatalogsCall.callAndGetReturnData(productCatalogsBean);
			//check 请求返回code		
			Logger.verifyEquals(result.getInt("Code"), 200, "响应报文-返回码");
			JSONObject jsonBeanRequest = result.getJSONObject("JsonBeanRequest");
			
			//sql & mongo 断言
			updateProductCatalogsCall.checkUpdateProductCatalogs(jsonBeanRequest);		
			
			if (YmtMongoDriverFactroy.isNewJavaMgoCheck()){
				NewJavaMongoCheckService.checkProductMgoSyncBySQL_NewJavaMgo(productID);
			}
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}

	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_UpdateProductCatalogs_002() {
		Logger.start(true,"修改FBX商品价格、库存");
		try {
			//定义期望值
			int Num=16;
			double QuotePrice=100.00;
			double VipPrice =20.00;
			double NewCustomerPrice=30.00; 
			
			//添加FBX商品 
			String productID =  SellerProductCallService.addFBXProduct(MapUtil.hashMap("LoginId", sellerName, "UserId", sellerId));
			Logger.verifyNotNull(productID, "响应报文-商品ID");
			
			//获取CatalogsID 
			String catalogsID = new YmtCatalogsIWapper().selectCatalogsByProductId(productID).get(0).getsCatalogId();
			Logger.verifyNotNull(catalogsID, "响应报文-CatalogID");			
							
			//修改库存
			//参数赋值
			Map<String, Object> updateMap = new HashMap<String, Object>();
			updateMap.put("Catalogs[0]->UserId", sellerId);			
			updateMap.put("UserID", sellerId);
			updateMap.put("ProductID", productID);
			updateMap.put("Catalogs[0]->CatalogId", catalogsID);	
			updateMap.put("Catalogs[0]->Num", Num);
			updateMap.put("Catalogs[0]->Price", QuotePrice);
			updateMap.put("Catalogs[0]->VipPrice", VipPrice);
			updateMap.put("Catalogs[0]->NewCustomerPrice", NewCustomerPrice);
						
			JSONObject productCatalogsBean = JsonBeanHelper.getJsonBean(JsonBeanFile.updateProductCatalogsTpl);
			JsonBeanHelper.updateJsonBean(productCatalogsBean, updateMap);
								
			//call			
			JSONObject result = updateProductCatalogsCall.callAndGetReturnData(productCatalogsBean);
			//check 请求返回code		
			Logger.verifyEquals(result.getInt("Code"), 200, "响应报文-返回码");
			JSONObject jsonBeanRequest = result.getJSONObject("JsonBeanRequest");
			
			//sql & mongo 断言
			updateProductCatalogsCall.checkUpdateProductCatalogs(jsonBeanRequest);										
			
			if (YmtMongoDriverFactroy.isNewJavaMgoCheck()){
				NewJavaMongoCheckService.checkProductMgoSyncBySQL_NewJavaMgo(productID);
			}
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}	
	
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_UpdateProductCatalogs_003() {
		Logger.start(true,"修改单组双规格商品规格价格、库存");
		try {
			//定义期望值
			int Num=16;
			double QuotePrice=100.00;
			double VipPrice =20.00;
			double NewCustomerPrice=30.00; 
						
			//添加FBX商品 
			String productID=  AddNewSellerProductCall.addNewProductTWOCatalog(MapUtil.hashMap("LoginId", sellerName, "UserId", sellerId)).getJSONObject("Data").getString("ProductID");
			
			//获取CatalogsID 
			String catalogsID1 = new YmtCatalogsIWapper().selectCatalogsByProductId(productID).get(0).getsCatalogId();
			String catalogsID2 = new YmtCatalogsIWapper().selectCatalogsByProductId(productID).get(1).getsCatalogId();
							
			//修改库存
			//参数赋值
			Map<String, Object> updateMap = new HashMap<String, Object>();					
			updateMap.put("UserID", sellerId);
			updateMap.put("Catalogs[0]->UserId", sellerId);	
			updateMap.put("Catalogs[1]->UserId", sellerId);	
			updateMap.put("ProductID", productID);
			updateMap.put("Catalogs[0]->ProductId", productID);
			updateMap.put("Catalogs[1]->ProductId", productID);
			updateMap.put("Catalogs[0]->CatalogId", catalogsID1);	
			updateMap.put("Catalogs[1]->CatalogId", catalogsID2);
			updateMap.put("Catalogs[0]->Num", Num);
			updateMap.put("Catalogs[0]->Price", QuotePrice);
			updateMap.put("Catalogs[0]->VipPrice", VipPrice);
			updateMap.put("Catalogs[0]->NewCustomerPrice", NewCustomerPrice);
						
			JSONObject productCatalogsBean = JsonBeanHelper.getJsonBean("com.ymatou.iapi.sellerproduct/UpdateProductCatalogs/UpdateProductCatalogs2.json");
			JsonBeanHelper.updateJsonBean(productCatalogsBean, updateMap);
								
			//call			
			JSONObject result = updateProductCatalogsCall.callAndGetReturnData(productCatalogsBean);
			//check 请求返回code		
			Logger.verifyEquals(result.getInt("Code"), 200, "响应报文-返回码");
			JSONObject jsonBeanRequest = result.getJSONObject("JsonBeanRequest");
			
			//sql & mongo 断言
			updateProductCatalogsCall.checkUpdateProductCatalogs(jsonBeanRequest);										
			
			if (YmtMongoDriverFactroy.isNewJavaMgoCheck()){
				NewJavaMongoCheckService.checkProductMgoSyncBySQL_NewJavaMgo(productID);
			}
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}	
	
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_UpdateProductCatalogs_004() {
		Logger.start(true,"修改活动中商品的价格、库存");
		try {
			//定义期望值
			int Num=16;
			double QuotePrice=100.00;
			double VipPrice =20.00;
			double NewCustomerPrice=30.00; 
			
			//添加商品 -定时上架			
			
			String productID = SellerProductCallService.addNewProductAndApproveActivity(sellerId, sellerName, activityUser, activityPswd, activityStartId, null);
			
			//获取CatalogsID 
			String catalogsID = new YmtCatalogsIWapper().selectCatalogsByProductId(productID).get(0).getsCatalogId();
			Logger.verifyNotNull(catalogsID, "响应报文-CatalogID");			
							
			//修改库存
			//参数赋值
			Map<String, Object> updateMap = new HashMap<String, Object>();
			updateMap.put("Catalogs[0]->UserId", sellerId);			
			updateMap.put("UserID", sellerId);
			updateMap.put("ProductID", productID);
			updateMap.put("Catalogs[0]->CatalogId", catalogsID);	
			updateMap.put("Catalogs[0]->Num", Num);
			updateMap.put("Catalogs[0]->Price", QuotePrice);
			updateMap.put("Catalogs[0]->VipPrice", VipPrice);
			updateMap.put("Catalogs[0]->NewCustomerPrice", NewCustomerPrice);
						
			JSONObject productCatalogsBean = JsonBeanHelper.getJsonBean(JsonBeanFile.updateProductCatalogsTpl);
			JsonBeanHelper.updateJsonBean(productCatalogsBean, updateMap);
								
			//call			
			JSONObject result = updateProductCatalogsCall.callAndGetReturnData(productCatalogsBean);
			//check 请求返回code		
			Logger.verifyEquals(result.getInt("Code"), 200, "响应报文-返回码");
			JSONObject jsonBeanRequest = result.getJSONObject("JsonBeanRequest");
			
			//sql & mongo 断言
			updateProductCatalogsCall.checkUpdateProductCatalogs(jsonBeanRequest);						
			
			if (YmtMongoDriverFactroy.isNewJavaMgoCheck()){
				NewJavaMongoCheckService.checkProductMgoSyncBySQL_NewJavaMgo(productID);
			}
			
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}	
	

	
	
	
	
	
}